package com.example.goodtrash.mine;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.goodtrash.R;
import com.example.goodtrash.db.RecycleDB;
import com.example.goodtrash.model.Recycle;
import com.google.android.material.appbar.MaterialToolbar;

import java.util.ArrayList;
import java.util.List;

public class recycleHistory extends AppCompatActivity {
    private List<Recycle> dataList=new ArrayList<>();
    private RecycleAdapter adapter;
    private RecyclerView recyclerView;
    private MaterialToolbar toolbar;
    private RecycleDB db;
    private LinearLayout empty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_history);
        empty= findViewById(R.id.lay_empty_data);
        recyclerView=findViewById(R.id.rv_history);
        toolbar=findViewById(R.id.toolbar_history);
        db= RecycleDB.getInstance(recycleHistory.this);
        adapter=new RecycleAdapter(dataList,recycleHistory.this,recycleHistory.this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        Button button=findViewById(R.id.bt_remove_all);
        showRecycleData();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Recycle> list=db.loadRecycle();
                for (Recycle recycle:list){
                    db.removeRecycle(recycle);
                }
                showRecycleData();
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    public void showRecycleData(){
        List<Recycle> recycleList=db.loadRecycle();
        if (recycleList.size()>0){
            dataList.clear();
            for (Recycle recycle:recycleList){
                dataList.add(recycle);
            }
            adapter.notifyDataSetChanged();
            empty.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }else {
            empty.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }
    }
}